package c.x.jy.service.service.impl;

import c.x.jy.common.exception.RRException;
import c.x.jy.dao.dto.OrderAwnserEntityDto;
import c.x.jy.dao.entity.TeacherConfigEntity;
import c.x.jy.service.service.TeacherConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;

import c.x.jy.dao.dao.OrderAwnserDao;
import c.x.jy.dao.entity.OrderAwnserEntity;
import c.x.jy.service.service.OrderAwnserService;
import org.apache.commons.lang3.StringUtils;


@Service("orderAwnserService")
public class OrderAwnserServiceImpl extends ServiceImpl<OrderAwnserDao, OrderAwnserEntity> implements OrderAwnserService {

    @Autowired
    private TeacherConfigService teacherConfigService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<OrderAwnserEntity> query= 	 new QueryWrapper<OrderAwnserEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        if(params.get("orderId")!=null) 	query.eq("order_id", params.get("orderId"));
//        IPage<OrderAwnserEntity> page = this.page(
//                new Query<OrderAwnserEntity>().getPage(params),
//                 query
//        );
        IPage<OrderAwnserEntityDto> page =   this.getBaseMapper().querypage(new Query<OrderAwnserEntity>().getPage(params),  query);
        return new PageUtils(page);
    }

    @Override
    public List<OrderAwnserEntity> getByOrderId(Long orderId) {
        QueryWrapper<OrderAwnserEntity> query= 	 new QueryWrapper<OrderAwnserEntity>() ;
        query.lambda().eq(OrderAwnserEntity::getOrderId,orderId);
        List<OrderAwnserEntity> l = this.getBaseMapper().selectList(query);
        return l;
    }

    @Override
    public List<OrderAwnserEntity> getReaskList(OrderAwnserEntity orderAwnser) {
        QueryWrapper<OrderAwnserEntity> query= 	 new QueryWrapper<OrderAwnserEntity>() ;
        query.lambda().eq(OrderAwnserEntity::getOrderId,orderAwnser.getOrderId());
        query.lambda().eq(OrderAwnserEntity::getMemberId,orderAwnser.getMemberId());
        List<OrderAwnserEntity> l = this.getBaseMapper().selectList(query);
        return l;
    }


    @Override
    public void reask(OrderAwnserEntity orderAwnser) {
        List<OrderAwnserEntity> awnsers = getReaskList(orderAwnser);
        if(orderAwnser.getTeacherId()==null){
            throw  new RRException("获取老师ID失败");
        }
        TeacherConfigEntity teacherConfigEntity = teacherConfigService.getByTeacherId(orderAwnser.getTeacherId());
        Integer reaskTimes = 2;
        if(teacherConfigEntity!=null){
            reaskTimes = teacherConfigEntity.getFreeReaskTimes();
        }else{
            reaskTimes = 2;
        }

        if(awnsers.size()>reaskTimes){
            throw  new RRException("追问次数超过限制："+reaskTimes+"次");

        }else{
            save(orderAwnser);
        }
    }

    @Override
    public OrderAwnserEntity selectOne(QueryWrapper<OrderAwnserEntity> query) {
        return this.getBaseMapper().selectOne(query);
    }
}
